﻿using Common;
using STMS.BLL;
using STMS.Models.DModels;
using STMS.WSTMSystem.FModels;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace STMS.WSTMSystem.Store
{
    public partial class FrmStoreRegionInfo : Form
    {
        public FrmStoreRegionInfo()
        {
            InitializeComponent();
        }
        public event Action ReloadStoreList;//刷新仓库列表页
        public event Action ReloadRegionList;//刷新仓库分区列表页
        FInfoData fModel = null;
        int sRegionId = 0;//当前的分区编号
        StoreRegionBLL sRegionBLL = new StoreRegionBLL();
        string oldName = "";

        private void FrmStoreRegionInfo_Load(object sender, EventArgs e)
        {
            if (this.Tag != null)
            {
                fModel = this.Tag as FInfoData;
                InitSRegionInfo();
            }
        }

        private void InitSRegionInfo()
        {
            if (fModel != null)
            {
                cboStores.LoadStores();//加载仓库列表
                string pageTypeName = "新增";
                switch (fModel.ActType)
                {
                    case 1:
                        ClearInfo();
                        btnClear.Enabled = true;
                        pageTypeName = "新增";
                        break;
                    case 2:
                        sRegionId = fModel.FId;
                        panelTempRange.Visible = true;
                        LoadSRegionInfo(sRegionId);
                        btnClear.Enabled = false;
                        pageTypeName = "修改";
                        break;
                    case 3:
                        ClearInfo();
                        cboStores.SelectedValue = fModel.FId;
                        cboStores.Enabled = false;
                        btnClear.Enabled = false;
                        pageTypeName = "添加分区";
                        break;
                    default: break;
                }
                this.Text += "----" + pageTypeName;
            }
        }

        /// <summary>
        /// 加载指定的区域信息
        /// </summary>
        /// <param name="regionId"></param>
        private void LoadSRegionInfo(int regionId)
        {
            StoreRegionInfo regionInfo = sRegionBLL.GetRegion(regionId);
            if (regionInfo != null)
            {
                txtSRegionNo.Text = regionInfo.SRegionNo;
                txtSRegionName.Text = regionInfo.SRegionName;
                txtHighTemper.Text = regionInfo.AllowHighTemperature == null ? "" : regionInfo.AllowHighTemperature.ToString();
                txtLowTemper.Text = regionInfo.AllowLowTemperature == null ? "" : regionInfo.AllowLowTemperature.ToString();
                txtSRTemperature.Text = regionInfo.SRTemperature == null ? "" : regionInfo.SRTemperature.ToString();
                txtRemark.Text = regionInfo.Remark;
                cboStores.SelectedValue = regionInfo.StoreId;
                oldName = regionInfo.SRegionName;
            }
        }

        private void ClearInfo()
        {
            txtSRegionNo.Clear();
            txtSRegionName.Clear();
            panelTempRange.Visible = false;
            txtSRTemperature.Clear();
            txtRemark.Clear();
            cboStores.SelectedValue = 0;
        }

        /// <summary>
        /// 保存分区
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            string sRegionName = txtSRegionName.Text.Trim();
            string sRegionNo = txtSRegionNo.Text.Trim();
            string remark = txtRemark.Text.Trim();
            decimal? srTemper = null;
            if (!string.IsNullOrEmpty(txtSRTemperature.Text))
                srTemper = txtSRTemperature.Text.GetDecimal();
            int storeId = cboStores.SelectedValue.GetInt();
            if (CheckSRegionInfo(sRegionName, storeId) == false) return;
            StoreRegionInfo regionInfo = new StoreRegionInfo()
            {
                SRegionId = sRegionId,
                SRegionName = sRegionName,
                SRegionNo = sRegionNo,
                SRTemperature = srTemper,
                Remark = remark,
                StoreId = storeId
            };
            bool bl = false;
            if (fModel.ActType == 1 || fModel.ActType == 3)
            {
                int reNewId = sRegionBLL.AddStoreRegionWithId(regionInfo);
                if (reNewId > 0)
                {
                    bl = true;
                    sRegionId = reNewId;
                }
            }
            else
            {
                decimal? highTemper = null;
                if (!string.IsNullOrEmpty(txtHighTemper.Text))
                    highTemper = txtHighTemper.Text.GetDecimal();
                decimal? lowTemper = null;
                if (!string.IsNullOrEmpty(txtLowTemper.Text))
                    lowTemper = txtLowTemper.Text.GetDecimal();
                regionInfo.AllowHighTemperature = highTemper;
                regionInfo.AllowLowTemperature = lowTemper;
                if (srTemper != null)
                {
                    if (lowTemper != null && highTemper != null && highTemper < lowTemper)
                    {
                        MsgBoxHelper.MsgErrorShow("仓库分区信息", "分区的低温必须小于高温！");
                        return;
                    }
                    if (lowTemper != null && srTemper < lowTemper)
                        regionInfo.TemperState = 0;
                    else if (highTemper != null && srTemper > highTemper)
                        regionInfo.TemperState = 2;
                    else if (lowTemper != null && highTemper != null && lowTemper <= srTemper && srTemper <= highTemper)
                        regionInfo.TemperState = 1;

                }
                bl = sRegionBLL.UpdateStoreRegion(regionInfo);
            }
            string msgTitle = "仓库分区信息";
            string typeName = (fModel.ActType == 1 || fModel.ActType == 3) ? "新增" : "修改";
            if (bl)
            {
                MsgBoxHelper.MsgBoxShow(msgTitle, $"分区:{sRegionName} {typeName} 成功！");
                if (fModel.ActType == 1)
                    fModel.ActType = 2;
                if (fModel.ActType != 3)
                    ReloadRegionList?.Invoke();
                else
                    ReloadStoreList?.Invoke();
            }
            else
            {
                MsgBoxHelper.MsgBoxShow(msgTitle, $"分区:{sRegionName} {typeName} 失败！");
                return;
            }
        }

        /// <summary>
        /// 检查分区信息
        /// </summary>
        /// <param name="sRegionName"></param>
        /// <param name="storeId"></param>
        /// <returns></returns>
        private bool CheckSRegionInfo(string sRegionName, int storeId)
        {
            string msgTitle = "提交仓库分区信息";
            if (string.IsNullOrEmpty(sRegionName))
            {
                MsgBoxHelper.MsgErrorShow(msgTitle, "分区名称不能为空！");
                return false;
            }
            if (oldName != "" && oldName != sRegionName || oldName == "")
            {
                if (sRegionBLL.ExistsSRegion(sRegionName))
                {
                    MsgBoxHelper.MsgErrorShow(msgTitle, "分区名称已存在！");
                    return false;
                }
            }
            if (storeId == 0)
            {
                MsgBoxHelper.MsgErrorShow(msgTitle, "请选择所属仓库！");
                return false;
            }
            return true;
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearInfo();
            fModel.ActType = 1;
            sRegionId = 0;
            oldName = "";
        }
    }
}
